package pe.com.bn.sach.common;

import java.util.Hashtable;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;

import org.apache.log4j.Logger;


public class Correo {

	private static Logger log = Logger.getLogger(Correo.class.getName());
	
	private String  subject = "";
	private String  text 	= "";
	private String  to 		= "";
	private String  cc 		= "";
			
	public void Enviarcorreo(){

		ResourceBundle rb = ResourceBundle.getBundle("parametro");
	    
		String mailHostSMTP = "mail.smtp.host="+rb.getString("MailHostSMTP").trim();
	    String cuentaMail = rb.getString("MailCuentaMail").trim();

		NNotificacion mail = new NNotificacion();
		try{
			
	

	    Vector toCorreo=null;
	    Vector ccCorreo=null;		

	    
		//Verificando si existe el separador en to
	    if(!to.trim().equals("")){
			if(to.indexOf(";")>=0){
				toCorreo=Split(to,";");
			}else{
				if(to.indexOf(",")>=0){
					toCorreo=Split(to,",");
				}else{
				    toCorreo=Split(to,"*");
				}
			}
	    }else{
	    	return;
	    }	

		//Verificando si existe el separador en to
	    if(!cc.trim().equals("")){	    
			if(cc.indexOf(";")>=0){
				ccCorreo=Split(cc,";");
			}else{
				if(cc.indexOf(",")>=0){
					ccCorreo=Split(cc,",");
				}else{
					ccCorreo=Split(cc,"*");
				}
			}
	    }
	    
		String mensaje = "";

	    Address[] addto = null;
	    Address[] addcc = null;

          int iNumCC=0;
          if(ccCorreo!=null)
            iNumCC=ccCorreo.size();

          addto = new Address[toCorreo.size()];
          addcc = null;
	           
	      if(iNumCC>0)
	      	addcc = new Address[iNumCC];
	          
	     for (int i = 0; i < toCorreo.size(); i++){
	     	addto[i] = new InternetAddress(toCorreo.get(i).toString());
	     };
	           
	     for (int j = 0; j < iNumCC; j++){
	     	addcc[j] = new InternetAddress(ccCorreo.get(j).toString());
	     };         
	    
	    Hashtable infoMail = new Hashtable(); 
	    
	    int res =99; 
	    res = mail.enviarEmail(
	    		mailHostSMTP
	    		,cuentaMail
				,addto
				,addcc
				,subject
				,text
				,infoMail);
		
	    //("Respuesta Mail1:"+res);
	    Address[] replicar = (Address[])infoMail.get("mails");
	    
	    int res1 =99; 
	    if (res == -20)
	    	res1 = mail.enviarEmail(
		    		mailHostSMTP
		    		,cuentaMail
					,replicar
					,null
					,subject
					,text
					,null);
	    
	  //("Respuesta Mail2:"+res1);
	    
		//("Mail Enviado");
		
		}
		catch(Exception e){
			log.error("Error:"+e.getMessage());
		}
	    
	    
	}

	
	public static Vector Split(String sCadenaValores,String sSeparador){
	   if(sCadenaValores==null)
	    return null;

	   if(sCadenaValores.trim().equals(""))
	    return null;

	   Vector objVector = new Vector();
	   int iCantTokens =0,iNumToken=0;
	   
	   StringTokenizer objToken = new StringTokenizer(sCadenaValores,sSeparador);
	   iCantTokens = objToken.countTokens();

	   for(iNumToken=0;iNumToken<iCantTokens;iNumToken++)
	      objVector.add(objToken.nextToken() );
	   
	   return objVector;
	}

	public String getCc() {
		return cc;
	}
	public void setCc(String cc) {
		this.cc = cc;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	
}
